<template>
<div class="login_container"> <!--登录页面-->
  <div class="login_box"> <!--登录框-->
<!--表单区域-->
    <el-form ref="loginFormRef" :rules= "loginRules"  :model="loginForm" label-width="80px" class="login_form">
      <!--用户名-->
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" prefix-icon="iconfont icon-yonghu1"></el-input>
      </el-form-item>
      <!--密码-->
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" prefix-icon="el-icon-unlock" type="password"></el-input>
      </el-form-item>
      <!--按钮-->
      <el-form-item  class="login_btn">
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="info" @click="resetLoginForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  name: "Login",
  data(){
    return{
      // 表单数据对象
      loginForm:{
        username:"admin",
        password:"123456",
      },
      // 验证对象
      loginRules:{
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 12, message: '长度在 5 ~ 12 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '密码为必填项', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 ~ 10 个字符', trigger: 'blur' },
        ],
      }
    }
  },
  methods:{
    resetLoginForm(){
      this.$refs.loginFormRef.resetFields();
    },
    // 登录方法
    login(){
      this.$refs.loginFormRef.validate(async valid =>{
        if (!valid) return;
/*        axios.get("http://localhost:9000/hello").then(function (response) {
          console.log(response);
        })*/
        const {data:res} = await this.$http.post("login",this.loginForm);
        if (res.flag == true){ // res为解析后的promise对象中的数据（后端返回值）
          this.$message.success("LoginSuccess");
          window.sessionStorage.setItem("user",res.user);
          await this.$router.push('/home')
          console.log(res.user);
        }else {
          this.$message.error("LoginFailed");
        }
      })
    },
  }
}
</script>

<style>
/*背景图片*/
.login_container{
  background-image:url("../assets/img/fav6.jpg");
  height: 100%;
}
/*登录盒子*/
.login_box{
  position: absolute;
  width: 400px;
  height:200px;
  top: 50%;
  left:50%;
  border-radius: 3px;
  transform: translate(-50%,-50%);
}
.login_btn{
  margin-left: 80px;
}
</style>